/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2019年1月7日
 */

package com.massyframework.beanskin.launcher.scanning.maven;

import java.util.Objects;

/**
 * {@link Coordinate},描述Maven项目的坐标
 */
public class Coordinate implements Comparable<Coordinate>{

    private static final String COLON = ":";
    private String groupId;
    private String artifactId;
    private String version;

    /**
     * 构造方法
     * @param builder  {@link Builder}
     */
    protected Coordinate(Builder builder) {
    	this.groupId = Objects.requireNonNull(builder.groupId, "\"groupId\" cannot be null.");
    	this.artifactId = Objects.requireNonNull(builder.artifactId, "\"artifactId\" cannot be null.");
    	this.version = Objects.requireNonNull(builder.version, "\"version\" cannot be null.");
    }
    

    /**
     * 插件所属组
     * @return {@link String}
     */
    public String getGroupId(){
        return this.groupId;
    }

    /**
     * 插件的编号
     * @return {@link String}
     */
    public String getArtifactId(){
        return this.artifactId;
    }

    /**
     * 插件的版本
     * @return {@link String}
     */
    public String getVersion(){
        return this.version;
    }


    public String getIdentifier(){
        return this.groupId.concat(COLON).concat(this.artifactId).concat(COLON).concat(this.version);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (o instanceof Coordinate) {
        	Coordinate that = (Coordinate) o;
            return Objects.equals(groupId, that.groupId) &&
                    Objects.equals(artifactId, that.artifactId) &&
                    Objects.equals(version, that.version);
        }
        return false;        
    }
    
    @Override
	public int compareTo(Coordinate o) {
		int result = this.groupId.compareTo(o.groupId);
		if (result == 0) {
			result = this.artifactId.compareTo(o.artifactId);
			
			if (result == 0) {
				result = this.version.compareTo(o.version);
			}
		}
		
		return result;
	}

    @Override
    public int hashCode() {
        return Objects.hash(groupId, artifactId, version);
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + 
                "{groupId='" + groupId + '\'' +
                ", artifactId='" + artifactId + '\'' +
                ", version='" + version + '\'' +
                '}';
    }
    
    /**
     * 创建构建器
     * @return {@link Builder}
     */
    public static Builder newBuilder() {
    	return new Builder();
    }
    
    /**
     * 使用{@code item}创建构建器
     * @param item {@link Coordinate}，坐标
     * @return {@link Builder}
     */
    public static Builder newBuilder(Coordinate item) {
    	return new Builder()
    			.groupId(item.groupId)
    			.artifactId(item.artifactId)
    			.version(item.version);
    }
    
    public static class Builder {
    	protected String groupId;
    	protected String artifactId;
    	protected String version;
    	
    	/**
    	 * 设置GroupId
    	 * @param groupId {@link String}
    	 * @return {@link Builder}
    	 */
    	public Builder groupId(String groupId) {
    		this.groupId = groupId;
    		return this;
    	}
    	
    	/**
    	 * 设置ArtifactId
    	 * @param artifactId {@link String}
    	 * @return {@link Builder}
    	 */
    	public Builder artifactId(String artifactId) {
    		this.artifactId = artifactId;
    		return this;
    	}
    	
    	/**
    	 * 设置版本
    	 * @param version {@link String}
    	 * @return {@link Builder}
    	 */
    	public Builder version(String version) {
    		this.version = version;
    		return this;
    	}
    	
    	/**
    	 * 创建maven坐标
    	 * @return {@link Coordinate}
    	 */
    	public Coordinate build() {
    		return new Coordinate(this);
    	}
    }

	
}
